/*
 * Group 3, User Interface Programming II (vt11)
 * Pooya | Flora | Monica | Can
 */
package vical.view;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.Calendar;
import java.util.List;
import java.util.Locale;
import javax.swing.BorderFactory;
import javax.swing.BoxLayout;
import javax.swing.JLabel;
import javax.swing.JPanel;
import vical.controller.PreferenceController;
import vical.model.Entry;

/**
 * This is a single column for DayView or WeekView.
 * It includes a caption and global entries (which are set to all day)
 * 
 * @author Can
 */
public class DayBoardHeader extends JPanel {

    private List<Entry> allDayEntries;
    private Calendar date;
    // components
    private JLabel dateLabel;
    private JPanel entriesPanel;
    private WeekView weekView;

    public DayBoardHeader(WeekView weekView, List<Entry> allDayEntries, Calendar day) {
        this.weekView = weekView;
        this.allDayEntries = allDayEntries;
        this.date = (Calendar) day.clone();
        initComponents();
    }

    private void initComponents() {
        Locale locale = PreferenceController.getLocale();
        String dateLabelString = date.getDisplayName(Calendar.DAY_OF_WEEK,
                Calendar.LONG, locale);
        dateLabelString += " (" + date.getDisplayName(Calendar.MONTH, Calendar.SHORT, locale);
        dateLabelString += " " + date.get(Calendar.DATE) + ")";
        dateLabel = new JLabel(dateLabelString);
        dateLabel.setBorder(BorderFactory.createLineBorder(Color.white));
        dateLabel.setOpaque(true);
        dateLabel.setBackground(Color.white);
        int dayOfWeek = date.get(Calendar.DAY_OF_WEEK);
        if (dayOfWeek == Calendar.SATURDAY || dayOfWeek == Calendar.SUNDAY) {
            dateLabel.setForeground(Color.red);
        }
        dateLabel.addMouseListener(new MouseAdapter() {

            @Override
            public void mouseEntered(MouseEvent e) {
                if (e.getSource() instanceof JLabel) {
                    ((JLabel) e.getSource()).setBorder(BorderFactory.createLineBorder(Color.yellow));
                }
                super.mouseEntered(e);
            }

            @Override
            public void mouseExited(MouseEvent e) {
                if (e.getSource() instanceof JLabel) {
                    ((JLabel) e.getSource()).setBorder(BorderFactory.createLineBorder(Color.white));
                }
                super.mouseExited(e);
            }
        });

        entriesPanel = new JPanel();
        entriesPanel.setLayout(new BoxLayout(entriesPanel, BoxLayout.Y_AXIS));
        for (final Entry entry : allDayEntries) {
            JLabel entryLabel = new JLabel(entry.getName());
            entryLabel.setOpaque(true);
            entryLabel.setToolTipText(entry.getTaskDetailedString());
            entryLabel.addMouseListener(new MouseAdapter() {

                @Override
                public void mouseEntered(MouseEvent e) {
                    if (e.getSource() instanceof JLabel) {
                        ((JLabel) e.getSource()).setBackground(Color.GRAY);
                    }
                    super.mouseEntered(e);
                }

                @Override
                public void mouseExited(MouseEvent e) {
                    if (e.getSource() instanceof JLabel) {
                        ((JLabel) e.getSource()).setBackground(null);
                    }
                    super.mouseExited(e);
                }

                @Override
                public void mouseClicked(MouseEvent e) {
                    EntryDialog entryDialog = new EditEntryDialog(entry);
                    entryDialog.setVisible(true);
                    DayBoardHeader.this.weekView.refresh();
                    DayBoardHeader.this.weekView.notifyEntryChanged();
                    super.mouseClicked(e);
                }
            });
            entriesPanel.add(entryLabel);
        }

        this.setLayout(new BorderLayout());
        this.add(BorderLayout.NORTH, dateLabel);
        this.add(BorderLayout.CENTER, entriesPanel);
        this.setBorder(BorderFactory.createLineBorder(Color.GRAY));
    }
}
